package org.jeecg.modules.bobo.wechat.common.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.bobo.common.service.ILbAutoServiceLogService;
import org.jeecg.modules.bobo.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: jeecg-boot-parent
 * @description: MP_OAuth2网页授权
 * @author: 三千霜
 * @create: 2020-10-12 11:37
 */
@Slf4j
@RestController
@RequestMapping("api/wechart/mpOauth2")
@Api(tags="MP_OAuth2网页授权接口")
public class WxMpOAuth2ApiController {

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private ILbAutoServiceLogService autoServiceLogService;

    @RequestMapping(value = "/getAuthorizationUrl", method = RequestMethod.GET)
    @ApiOperation("获取网页授权url")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "url", paramType = "query", dataType = "String", required = true),
    })
    public Result<String> getAuthorizationUrl(@RequestParam("url") String url)
    {
        String  authorizationUrl= wxMpService.getOAuth2Service().buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
        return Result.success(authorizationUrl,authorizationUrl);
    }

    //获得用户基本信息
    @RequestMapping(value = "/getWxMpUser", method = RequestMethod.GET)
    @ApiOperation("获得用户基本信息")
    public Result<WxOAuth2UserInfo> getWxMpUser(WxOAuth2AccessToken wxMpOAuth2AccessToken)
    {
        WxOAuth2UserInfo wxMpUser = null;
        try {
            wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return Result.success(wxMpUser);
    }
//    刷新access token

//    验证access token
    @RequestMapping(value = "/getOpenidAndAccessTokenFromCode", method = RequestMethod.GET)
    @ApiOperation("获取用户唯一标识 以及用户token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "url获取code", paramType = "query", dataType = "String", required = true),
    })
    /**
     * 参考 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
     */
    public Result<WxOAuth2AccessToken> getOpenidAndAccessTokenFromCode(@RequestParam("code") String code)
    {
        WxOAuth2AccessToken wxMpOAuth2AccessToken = new WxOAuth2AccessToken();
        log.info("微信获取用户唯一标识 以及用户token接受的code:"+code);
        try {
            wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            log.info("微信获取用户唯一标识 以及用户token"+wxMpOAuth2AccessToken.getOpenId());
            if (StringUtils.isNotBlank(wxMpOAuth2AccessToken.getOpenId())){
                return Result.success(wxMpOAuth2AccessToken);
            }else{
                return Result.error("未成功取得openid,请退出公众号重试！");
            }
        } catch (WxErrorException e) {
            log.info("微信获取用户唯一标识 以及用户token错误"+e.getMessage());
            e.printStackTrace();
            autoServiceLogService.setLog("微信获取用户唯一标识 以及用户token错误",code,e.getMessage());
            return Result.error(e.getMessage());
        }

    }
    @RequestMapping(value = "/createJsapiSignature", method = RequestMethod.GET)
    @ApiOperation("创建调用jsapi时所需要的签名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "url必须是调用JS接口页面的完整URL", paramType = "query", dataType = "String", required = true),
    })
    public Result<WxJsapiSignature> createJsapiSignature(@RequestParam("url") String url)
    {
        try {
            return Result.success(wxMpService.createJsapiSignature(url));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return Result.error("失败");
        }
    }
}
